//
//  HEPersonDataViewController.m
//  HealthFamily
//
//  Created by LOOK on 2020/12/27.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEPersonDataViewController.h"
#import "HEReportSelectCell.h"

@interface HEPersonDataViewController ()<UITableViewDelegate, UITableViewDataSource, HEReportSelectCellDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, assign) BOOL hasChange;

@end

@implementation HEPersonDataViewController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [self updateUserInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"个人资料";
     
    [self initUI];
    [self initCell];
}

- (void)initUI {
   _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight- ZXNavBarHeight - ZXSafeAreaBottom) style:UITableViewStyleGrouped];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    
   _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   _tableView.scrollsToTop = YES;
   _tableView.tableFooterView = [[UIView alloc] init];
    
    [_tableView registerClass:HEReportSelectCell.class forCellReuseIdentifier:@"HEReportSelectCell"];
   
   self.tableView.backgroundColor = KWhiteColor;
   self.tableView.delegate = self;
   self.tableView.dataSource = self;
   [self.view addSubview:self.tableView];
}

- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];
    NSMutableArray *sec1 = [NSMutableArray array];
    NSMutableArray *sec2 = [NSMutableArray array];
    
    UserInfo *user =  [UserManager sharedUserManager].curUserInfo;
//    HEHealthArchiveModel *realModel =  [UserManager sharedUserManager].archiveInfo;

    self.formModel.pi_realName = _userModel.realname;
    self.formModel.pi_sex = [HEEnums sexToText: _userModel.sex];
    self.formModel.pi_birthd = _userModel.birth_date;
    self.formModel.pi_age = _userModel.Kage;
    self.formModel.pi_cardType = @"身份证";
    self.formModel.pi_cardCode = @"";

    self.formModel.pi_hyzk = _model.user_info.hyzkmc;
    self.formModel.pi_mz = _model.user_info.mzmc;
    self.formModel.pi_whcd = _model.user_info.whcdmc;
    self.formModel.pi_zy = _model.user_info.zymc;
    self.formModel.pi_snqk = _model.user_info.snqk;
    self.formModel.pi_cjqk = _model.user_info.cjqkmc;
    
    self.formModel.pi_jzqk = _model.user_basic.jzzk;
    self.formModel.pi_jjly = _model.user_basic.jjqk;
    self.formModel.pi_ysr = _model.user_basic.yjjsr;
    self.formModel.pi_ylbz = _model.user_basic.ybzfmc;

    NSArray *secData1 = @[
        @{@"name":@"姓名", @"key":@"pi_realName"},
        @{@"name":@"性别", @"key":@"pi_sex"},
        @{@"name":@"生日", @"key":@"pi_birthd"},
        @{@"name":@"年龄", @"key":@"pi_age"},
        @{@"name":@"证件类型", @"key":@"pi_cardType"},
        @{@"name":@"证件号码", @"key":@"pi_cardCode"},
    ];
    
    NSArray *secData2 = @[
        @{@"name":@"婚姻状况", @"key":@"pi_hyzk"},
        @{@"name":@"民族", @"key":@"pi_mz"},
        @{@"name":@"居住情况", @"key":@"pi_jzqk"},
        @{@"name":@"文化程度", @"key":@"pi_whcd"},
        @{@"name":@"职业", @"key":@"pi_zy"},
        @{@"name":@"经济来源", @"key":@"pi_jjly"},
        @{@"name":@"月收入", @"key":@"pi_ysr"},
        @{@"name":@"医疗保障", @"key":@"pi_ylbz"},
        @{@"name":@"失能情况", @"key":@"pi_snqk"},
        @{@"name":@"残疾情况", @"key":@"pi_cjqk"},

    ];
    
    NSString *mzStr = @"汉族、蒙古族、回族、藏族、维吾尔族、苗族、彝族、壮族、布依族、朝鲜族、满族、侗族、瑶族、白族、土家族、哈尼族、哈萨克族、傣族、黎族、僳僳族、佤族、畲族、高山族、拉祜族、水族、东乡族、纳西族、景颇族、柯尔克孜族、土族、达斡尔族、仫佬族、羌族、布朗族、撒拉族、毛南族、仡佬族、锡伯族、阿昌族、普米族、塔吉克族、怒族、乌孜别克族、俄罗斯族、鄂温克族、德昂族、保安族、裕固族、京族、塔塔尔族、独龙族、鄂伦春族、赫哲族、门巴族、珞巴族、基诺族";
    
    NSArray *pickerData = @[
    
        @[@"已婚", @"未婚", @"离异", @"丧偶", @"未说明的婚姻状况"],
        [mzStr componentsSeparatedByString:@"、"],
        @[@"独自居住", @"与配偶居住", @"与子女居住", @"与亲戚居住", @"居无定所", @"其他"],
        @[@"未读书", @"小学", @"初中", @"高中/技校/中专", @"大专", @"本科及以上"],
        @[@"公务员", @"教师", @"军人", @"事业职工", @"企业职工", @"农民", @"商人", @"无固定职业"],
        @[@"出租房子", @"子女赡养", @"做小生意", @"退休金/子女", @"退休金", @"养老金/子女", @"养老金", @"社保工资", @"退休金/养老金", @"低保", @"其他补助", @"亲友资助"],
        @[@"<3000元/月", @"3000~5000元/月", @"5000~10000元/月", @">10000元/月"],
        @[@"湖南省直医保",@"长沙地区职工医保",@"长沙地区居民医保",@"省内异地职工",@"省内异地城乡居民",@"长沙市离休医保",@"湖南省离休医保",@"跨省异地职工",@"跨省异地城乡居民",@"商业医保",@"自费",@"其他"],
        @[@"能力完好",@"轻度失能",@"，中度失能",@"重度失能"],
        @[@"无残疾",@"视力残疾",@"听力残疾",@"言语残疾",@"肢体残疾",@"智力残疾",@"精神残疾",@"其他残疾"]
    ];
    
    for (NSDictionary *dict in secData1) {
        GDFiterCellModel *realNameModel = [GDFiterCellModel new];
        realNameModel.cellKlass = HEReportSelectCell.class;
        realNameModel.leftTitle = dict[@"name"];
        realNameModel.placeHolder = @"";
        realNameModel.isCloesPK = YES;
        realNameModel.tableKey = dict[@"key"];
        [sec1 addObject:realNameModel];
    }
    
    NSInteger i = 0;
    for (NSDictionary *dict in secData2) {
        GDFiterCellModel *realNameModel = [GDFiterCellModel new];
        realNameModel.cellKlass = HEReportSelectCell.class;
        realNameModel.leftTitle = dict[@"name"];
        realNameModel.placeHolder = @"未填写";
        realNameModel.tableKey = dict[@"key"];
        realNameModel.pickerDataArray = pickerData[i];
        [sec2 addObject:realNameModel];
        i++;
    }
     
    [_sections addObject: sec1];
    [_sections addObject: sec2];

   [_tableView reloadData];
}
 
 
- (void)updateUserInfo {
    
    if (!_hasChange) {
        return;
    }
     
    NSMutableDictionary *dict = @{}.mutableCopy;
    NSMutableDictionary *user_basic = @{}.mutableCopy;
    NSMutableDictionary *user_info = @{}.mutableCopy;

    [user_basic setValue:_formModel.pi_ysr forKey:@"yjjsr"];
    [user_basic setValue:_formModel.pi_jzqk forKey:@"jzzk"];
    [user_basic setValue:_formModel.pi_ylbz forKey:@"ybzfmc"];
    [user_basic setValue:_formModel.pi_jjly forKey:@"jjqk"];
    
    [user_info setValue:_formModel.pi_snqk forKey:@"snqk"];
    [user_info setValue:_formModel.pi_mz forKey:@"mzmc"];
    [user_info setValue:_formModel.pi_whcd forKey:@"whcdmc"];
    [user_info setValue:_formModel.pi_hyzk forKey:@"hyzkmc"];
    [user_info setValue:_model.user_info.pid forKey:@"pid"];
    [user_info setValue:_formModel.pi_zy forKey:@"zymc"];
    [user_info setValue:_formModel.pi_cjqk forKey:@"cjqkmc"];

    [dict setValue:user_basic forKey:@"user_basic"];
    [dict setValue:user_info forKey:@"user_info"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KsaveAllInfo parameters:dict success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues: responseObject];
        if (response.success) {
            [Dialog toastBottom: @"修改成功"];
            
            if (self.backBlock) {
                self.backBlock();
            }
            // todo: 实名认证信息更新
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   NSMutableArray *sec = _sections[section];
   return sec.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSMutableArray *sec = _sections[indexPath.section];
   GDFiterCellModel *fiterModel = sec[indexPath.row];
   
   UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSMutableArray *sec = _sections[indexPath.section];
   GDFiterCellModel *fiterModel = sec[indexPath.row];

    if (fiterModel.cellType == GDFiterCellTypeSelect_ADDPHOTO ||
        fiterModel.cellType == GDFiterCellTypeSelect_TEXTView) {
        return fiterModel.cellHeight;
    }
   return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
              [cell refreshContent:fiterModel formModel:self.formModel indexPath:indexPath];
   }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSMutableArray *sec = _sections[indexPath.section];
  
}

  

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSMutableArray *sec = _sections[section];
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = CViewBgColor;
    return v;
}


#pragma mark - TextFiledDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    return YES;
}


#pragma mark - HEReportSelectCellDelegate
- (void)cellSelectHandler:(HEReportSelectCell *)cell index:(NSInteger)index{
    _hasChange = YES;
}

@end
 
